<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Model\CoinLog;
use App\Model\Gift;
use App\Model\GiftLog;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Layout\Content;

class GiftLogController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */

    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('礼物赠送记录');
            $content->description('');
            $content->body($this->grid());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(GiftLog::class, function (Grid $grid) {
            $where = [
                // ['user_id', '>', 0],
                // ['created_at', '>', date('Y-m-d', strtotime('-30 day'))]
            ];
            // if (!request()->dt) {
            //     request()->merge(['created_at' => ['start' => date('Y-m-d', strtotime('-7 day')), 'end' => '']]);
            // }
            $grid->model()->where($where)->orderBy('id', 'desc');
            $grid->id('记录ID');
            $grid->userProfile()->nick_name('赠送方')->display(function ($data) {
                return $data . "  ( Uid: {$this->userProfile->user_profile_id} )";
            });
            $grid->receiverProfile()->nick_name('接收方')->display(function ($data) {
                $user_profile_id = empty($this->receiverProfile->user_profile_id) ? '' : $this->receiverProfile->user_profile_id;
                return $data . "  ( Uid: ".$user_profile_id." )";
            });
            // $grid->userProfile()->user_profile_id('赠送方ID');
            // $grid->gift_id('礼物ID');
            $grid->giftInfo()->name('礼物')->display(function ($data) {
                return $data . "  ( ID: {$this->gift_id} )";
            });
            $grid->from('来源')->display(function ($data) {
                return GiftLog::FROM[$data] ?? '未知';
            });
            $grid->giftInfo()->type('礼物类型')->display(function ($data) {
                return Gift::TYPE[$data];
            });
            $grid->gift_num('礼物数量');
            $grid->gift_coin('礼物金币')->totalRow(function ($data) {
                return round($data, 1) . "金币";
            });
            $grid->created_at('赠送时间')->display(function ($time) {
                return !$time ? '-' : date('Y-m-d H:i:s', strtotime($time));
            });

            $grid->flag('记录状态')->display(function ($data) {
                return GiftLog::STATUS[$data];
            });

            $grid->fetch_at('领取时间')->display(function ($time) {
                return !$time ? '-' : date('Y-m-d H:i:s', strtotime($time));
            });

            //禁用行选择
            $grid->disableRowSelector();
            $grid->disableActions();
            $grid->disableCreateButton();
            $grid->filter(function ($filter) {
                $filter->expand();
                $filter->disableIdFilter();
                $filter->column(1 / 3, function ($filter) {
                    $filter->equal('gift_id', '礼物')->select(Gift::getList());
                    $filter->equal('giftInfo.type', '礼物类型')->select(GiftLog::TYPE);
                    $filter->equal('flag', '赠送状态')->select(GiftLog::STATUS);
                });
                $filter->column(1 / 3, function ($filter) {
                    $filter->equal('userProfile.user_profile_id', '赠送方ID');
                    $filter->between('created_at', '赠送时间')->datetime();
                });
                $filter->column(1 / 3, function ($filter) {
                    $filter->equal('receiverProfile.user_profile_id', '接收方ID');
                    $filter->equal('from', '来源')->select(GiftLog::FROM);
                });
            });
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableAll();
            });
        });
    }
}
